package com.ms.service.module.analysisgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import com.ms.game.itembox.ItemChangeWay;
import com.ms.service.module.analysisgroup.db.model.UsedCoinCardDetailLogModel;
import com.xpec.c4.game.utility.GameUtility;

public class UsedCoinCardData {

	/**未提供資訊時的參考字串*/
	public static final String UNKNOWN = "UNKNOWN";
	
	/**分割字元*/
	public static final String SPLIT_CHARACTER = "&&";
	
	/**統計*/
	private HashMap<String, Integer> counter = new HashMap<String, Integer>();
	
	/**
	 * 增加一個
	 */
	public void add(ItemChangeWay createWay, String createGameType, String createHallType){
		String counterKey = getCounterKey(createWay, createGameType, createHallType);
		Integer count = counter.get(counterKey);
		if(count == null){
			counter.put(counterKey, 1);
		}
		else{
			counter.put(counterKey, count + 1);
		}
	}
	
	/**
	 * 組成key
	 */
	public static String getCounterKey(ItemChangeWay createWay, String createGameType, String createHallType){
		String way = UNKNOWN;
		if(createWay != null){
			way = createWay.name();
		}
		
		String game = UNKNOWN;
		if(GameUtility.isHaveString(createGameType)){
			game = createGameType;
		}
		
		String hall = UNKNOWN;
		if(GameUtility.isHaveString(createHallType)){
			hall = createHallType;
		}
		
		return way + SPLIT_CHARACTER + game + SPLIT_CHARACTER + hall;
	}
	
	/**
	 * 取得統計內容models
	 */
	public ArrayList<UsedCoinCardDetailLogModel> getLogModels(int parValue){
		ArrayList<UsedCoinCardDetailLogModel> logModels = new ArrayList<UsedCoinCardDetailLogModel>();
		if(counter.size() > 0){
			for(Entry<String, Integer> entry : counter.entrySet()){
				try {
					UsedCoinCardDetailLogModel logModel = new UsedCoinCardDetailLogModel();
					logModel.setCoinCardParValue(parValue);
					
					String[] keys = entry.getKey().split(SPLIT_CHARACTER);
					
					logModel.setCreateWay(keys[0]);
					logModel.setCreateGameType(keys[1]);
					logModel.setCreateHallType(keys[2]);
					
					logModel.setCount(entry.getValue());
					
					logModels.add(logModel);
				} catch (Exception e) {
					continue;
				}

			}
		}
		
		return logModels;
	}
	
}
